import 'package:auto_size_text/auto_size_text.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_module/beans/conmmonResult.dart';
import 'package:flutter_module/http/api_service.dart';
import 'package:flutter_module/widget/view/LoadingDialog.dart';
import 'package:mqtt_client/mqtt_client.dart';
import 'package:flutter_module/commom/application.dart';
import 'package:flutter_module/commom/data/const.dart';
import 'package:flutter_module/commom/ui.dart';
import 'package:flutter_module/event/getSetting_event.dart';
import 'package:flutter_module/mqtt/Mqtt.dart';
import 'package:oktoast/oktoast.dart';

import '../../DemoLocalizations.dart';
import '../../beans/Entity.dart';
import 'RandomWordsState.dart';

/**
 * 机器安装设置
 */
class MachineSettings extends StatefulWidget {
  final App_esp_ip model;

  const MachineSettings({Key key, this.model}) : super(key: key);

  @override
  _MachineSettingsState createState() => _MachineSettingsState();
}

class _MachineSettingsState extends State<MachineSettings>
    with AutomaticKeepAliveClientMixin {
  String deviceType;

  List<double> _showsliderValue = [0.0, 0.0, 0.0, 0.0];

  List<double> _sliderValues1 = [8.0, 5.0, 8.0, 5.0];
  List<double> _sliderValues2 = [8.0, 5.0, 8.0, 5.0];
  List<double> _sliderValues3 = [2.0, 2.0, 2.0, 3.0, 1.8];
  List<double> _sliderValues4 = [100.0, 50.0, 100.0, 50.0];

  //getPopWindow(context,true,0) 用最后一个参数来获取弹出内容
  List<String> singleTitle1 = [
    '快速度开门1时间',
    '慢速度开门1时间',
    '快速度关门1时间',
    '慢速度关门1时间'
  ]; //对应一个选项item = title+value
  List<String> singleTitle2 = ['快速度开门2时间', '慢速度开门2时间', '快速度关门2时间', '慢速度关门2时间'];
  List<String> singleTitle3 = ['关门延时时间', '开延时', '关延时', '遇阻回弹力', '停门检测力'];
  List<String> singleTitle4 = ['快开速度', '慢开速度', '快关速度', '慢关速度'];

  List<double> _sliderValues5 = [100.0, 50.0];
  List<double> _sliderValues6 = [100.0, 50.0, 100.0, 50.0];
  List<String> doubleTitle1 = ['开门刹车距离', '关门刹车距离'];
  List<String> doubleTitle2 = ['快开门力', '慢开门力', '快关门力', '慢关门力'];
  List<Item> singleArgs = [];
  List<Item> doubleArgs = [];

  Data currentDeviceAllDatas;
  Mqtt demo = Mqtt.getInstance();

  bool seesetting = false;

  bool menyi = false;
  bool liduheyanshi = false;
  bool guanmensudu = false;

  bool mener = false;
  bool liduheyanchi = false;
  bool men2time = false;
  bool men2 = false;
  bool jiangetime = false;
  bool men_senven = false;
  TextEditingController _ApNamecontroller = TextEditingController();
  TextEditingController _ApPasswcontroller = TextEditingController();

  bool hasysy = false;

  String title_two;

  @override
  void initState() {
    super.initState();
    this.registerLoginEvent();
    deviceType = widget.model.espType;
    if (widget.model.level > 1) {
      hasysy = true;
    } else {
      hasysy = false;
    }
    if (!widget.model.share) {
      hasysy = true;
    }

    /*这里要延时加载  否则会抱The widget on which setState() or markNeedsBuild() was called was:错误*/
    Future.delayed(Duration(milliseconds: 500)).then((e) {
      this.showLoding();
      this.getSettingStatus();
    });

    switch (deviceType) {
      case "11":
      case "18":
        //直流平移门
        singleTitle3 = ['关门延时时间', 'null', 'null', '遇阻回弹力', '停门检测力'];
        break;
      case "22":
      case "28":
        //直流平开门
        men2 = true;
        men2time = true;
        break;
      case "33":
        singleTitle3 = ['关门延时时间', 'null', 'null', '遇阻回弹力', '停门检测力'];
        break;
      case "55":
      case "58":
        singleTitle3 = ['关门延时时间', 'null', 'null', '遇阻回弹力', '停门检测力'];
        break;
      case "66":
      case "68":
        men2 = true;
        men2time = true;
        break;
      case "77":
        singleTitle3 = ['关门延时时间', 'null', 'null', '遇阻回弹力', '停门检测力'];
        break;
    }
    singleArgs = [
      Item(_sliderValues1, singleTitle1),
      Item(_sliderValues2, singleTitle2),
      Item(_sliderValues3, singleTitle3),
      Item(_sliderValues4, singleTitle4),
      Item(_sliderValues5, doubleTitle1),
      Item(_sliderValues6, doubleTitle2)
    ];
    // doubleArgs =  [Item(_sliderValues5,doubleTitle1),Item(_sliderValues6,doubleTitle2)];
    currentDeviceAllDatas = Data(singleArgs, doubleArgs);
    //    screeDate();
  }

  @override
  void dispose() {
    super.dispose();
//    print(currentDeviceAllDatas);
//    print("dispose 页面销毁");
    singleArgs.clear();
    doubleArgs.clear();
    //    demo.disconnect();
  }

  @override
  Widget build(BuildContext context) {
    var screenWidth = MediaQuery.of(context).size.width;
    this.showItemWithType();
    return Scaffold(
        //      appBar:
        //      AppBar(
        //        title: Text('参数设置'),
        //      ),
    body:  Column(
        children: <Widget>[
          SingleChildScrollView(
            child: Column(
                      children: [
                         Card(
            margin: EdgeInsets.symmetric(horizontal: 2, vertical: 20),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Container(
                  padding: EdgeInsets.only(left: 20, top: 10, bottom: 10),
                  child: Row(
                    children: <Widget>[
                      Icon(
                        Icons.settings_applications,
                        color: Colors.blueAccent,
                      ),
                      //                    Icon(Icons.settings_brightness),
                      Text(
                        DemoLocalizations.of(context).ParamSetting,
                        style: TextStyle(
                            color: Colors.blueAccent,
                            fontWeight: FontWeight.bold),
                      )
                    ],
                  ),
                ),
                Container(
                  height: 1.0,
                  color: Colors.grey[400],
                ),
                Visibility(
                  visible: mener,
                  maintainSize: false,
                  child: ListTile(
                    onTap: () => getPopWindow(context, true, 0),
                    title: Text(DemoLocalizations.of(context).setdoor1),
                    trailing: Icon(Icons.keyboard_arrow_right),
                  ),
                ),
                Container(
                  height: 1.0,
                  color: Colors.grey[400],
                ),
                Visibility(
                  visible: liduheyanshi,
                  maintainSize: false,
                  child: ListTile(
                    onTap: () => getPopWindow(context, true, 2),
                    title: Text(title_two),
                    trailing: Icon(Icons.keyboard_arrow_right),
                  ),
                ),
                Container(
                  height: 1.0,
                  color: Colors.grey[400],
                ),
                Visibility(
                  visible: guanmensudu,
                  maintainSize: false,
                  child: ListTile(
                    onTap: () => getPopWindow(context, true, 3),
                    title: Text(DemoLocalizations.of(context).setdoorspeed),
                    trailing: Icon(Icons.keyboard_arrow_right),
                  ),
                ),
                Container(
                  height: 1.0,
                  color: Colors.grey[400],
                ),
                Visibility(
                  visible: men_senven,
                  maintainSize: false,
                  child: ListTile(
                    onTap: () => getPopWindow(context, true, 4),
                    title: Text('设置刹车距离'),
                    trailing: Icon(Icons.keyboard_arrow_right),
                  ),
                ),
                Container(
                  height: 1.0,
                  color: Colors.grey[400],
                ),
                Visibility(
                  visible: men_senven,
                  maintainSize: false,
                  child: ListTile(
                    onTap: () => getPopWindow(context, true, 5),
                    title: Text('设置开门力'),
                    trailing: Icon(Icons.keyboard_arrow_right),
                  ),
                ),
              ],
            ),
          ),
          Card(
            margin: EdgeInsets.symmetric(horizontal: 2, vertical: 30),
            child: Visibility(
              visible: men2,
              maintainSize: false,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.only(left: 20, top: 10, bottom: 10),
                    child: Row(
                      children: <Widget>[
                        Icon(
                          Icons.settings_applications,
                          color: Colors.blueAccent,
                        ),
                        //                    Icon(Icons.settings_brightness),
                        Text(
                          DemoLocalizations.of(context).ParamSetting + ' 2',
                          style: TextStyle(
                              color: Colors.blueAccent,
                              fontWeight: FontWeight.bold),
                        )
                      ],
                    ),
                  ),
                  Container(
                    height: 1.0,
                    color: Colors.grey[400],
                  ),
                  Visibility(
                    visible: men2time,
                    maintainSize: false,
                    child: ListTile(
                      onTap: () => getPopWindow(context, true, 1),
                      title: Text(DemoLocalizations.of(context).setdoor2),
                      trailing: Icon(Icons.keyboard_arrow_right),
                    ),
                  ),
                  Container(
                    height: 1.0,
                    color: Colors.grey[400],
                  ),
                  Visibility(
                    visible: jiangetime,
                    maintainSize: false,
                    child: ListTile(
                      onTap: () => getPopWindow(context, true, 2),
                      title: Text(DemoLocalizations.of(context).setInterval),
                      trailing: Icon(Icons.keyboard_arrow_right),
                    ),
                  ),
                ],
              ),
            ),
          ),
          
          ],
            ),
          ),
         Spacer(),
          Container(
            alignment: Alignment.center,
            margin: EdgeInsets.symmetric(horizontal: 1, vertical: 1),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                new Space(),
                Expanded(
                  child: OutlineButton(
                    padding: EdgeInsets.all(2),
                    textColor: Colors.black,
                    onPressed: () {
                      final MqttClientPayloadBuilder builder =
                          MqttClientPayloadBuilder();
                      builder.addByte(72);
                      builder.addByte(1);
                      builder.addByte(10);
                      builder.addByte(11);
                      demo.publishMessage(builder);
                      seesetting = true;
                    },
                    child: Row(
                      children: <Widget>[
                        Icon(
                          Icons.remove_red_eye,
                          color: Colors.blueAccent,
                        ),
                        AutoSizeText(
                          DemoLocalizations.of(context).Viewsettings,
                          textAlign: TextAlign.center,
                          // style: TextStyle(fontWeight: FontWeight.normal,fontSize: 2),
                        )
                      ],
                    ),
                  ),
                  flex: 1,
                ),
                new Space(),
                Expanded(
                  child: OutlineButton(
                    padding: EdgeInsets.all(2),
                    textColor: Colors.black,
                    onPressed: () {
                      final MqttClientPayloadBuilder builder =
                          MqttClientPayloadBuilder();
                      builder.addByte(0x48);
                      builder.addByte(0x4F);
                      builder.addByte(0x4C);
                      builder.addByte(0x45);
                      builder.addByte(0x41);
                      builder.addInt(16);

                      demo.publishMessage(builder);

                      showDialog(
                          context: context,
                          builder: (BuildContext context) {
                            return CupertinoAlertDialog(
                              title:
                                  Text(DemoLocalizations.of(context).reminder),
                              content: SizedBox(
                                child: Text(DemoLocalizations.of(context)
                                    .presscontrollearn),
                              ),
                              actions: <Widget>[
                                CupertinoDialogAction(
                                  child: Text(
                                      DemoLocalizations.of(context).confirm),
                                  onPressed: () {
//                                    print("确定");
                                    Navigator.pop(context);
                                  },
                                ),
                              ],
                            );
                          });
                    },
                    child: Row(
                      children: <Widget>[
                        Icon(
                          Icons.devices_other,
                          color: Colors.blue,
                        ),
                        AutoSizeText(
                          DemoLocalizations.of(context).Itinerary,
                          textAlign: TextAlign.center,
                          // style: TextStyle(fontWeight: FontWeight.normal,fontSize: 8),
                        )
                      ],
                    ),
                  ),
                  flex: 1,
                ),
                new Space(),
                Expanded(
                  child: Container(
                    alignment: Alignment.center,
                    child: OutlineButton(
                      padding: EdgeInsets.all(2),
                      onPressed: () {
                        showInputNewAP(context);
                      },
                      color: Colors.blueAccent,
                      // textColor: Colors.blueAccent,
                      disabledTextColor: Colors.blueAccent,
                      child: Container(
                        height: 30,
                        child: Row(
                          children: <Widget>[
                            Icon(
                              Icons.border_color,
                              color: Colors.blue,
                            ),
                            Expanded(
                              // Constrains AutoSizeText to the width of the Row
                              child: AutoSizeText(
                                  DemoLocalizations.of(context).ModifyAP,
                                  textAlign: TextAlign.center,
                                //  style: TextStyle(fontWeight: FontWeight.normal,fontSize: 8),
                                  ),
                                  
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                  flex: 1,
                ),
                new Space()
              ],
            ),
          ),
          SizedBox(
            height: 20,
          ),
          GestureDetector(
              onTap: () {
                final MqttClientPayloadBuilder builder =
                    MqttClientPayloadBuilder();
                builder.addByte(72);
                builder.addByte(2);
                print(currentDeviceAllDatas);
                for (var i = 0;
                    i < currentDeviceAllDatas.singleArgs.length;
                    i++) {
                  var value = currentDeviceAllDatas.singleArgs[i];
                  for (var j = 0; j < value.itemValue.length; j++) {
                    var lastValue = value.itemValue[j].toInt();
                    print(value.itemValue[j]);
                    if (i == 2 && j == 3) {
                      print("lastValue");
                      lastValue = (value.itemValue[j] * 10).toInt();
                      print(lastValue);
                    }
                    if (i == 2 && j == 4) {
                      print("lastValue");
                      lastValue = (value.itemValue[j] * 10).toInt();
                      print(lastValue);
                    }

                    builder.addByte(lastValue);
                  }
                }
                // for (var value in currentDeviceAllDatas.singleArgs) {
                //   print(value.itemTitle);
                //   print(value.itemValue);
                //   value.itemValue.forEach((f) {
                //     print(f);
                //     builder.addByte(f.toInt());
                //   });
                // }
                print("builder.length.toString()");
                print(builder.length.toString());
                print("builder.toString()");
                demo.publishMessage(builder);
                showToast(DemoLocalizations.of(context).SettingsSubmitted);
              },
              child: Container(
                width: screenWidth / 1.5,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: Colors.blueAccent,
                  borderRadius: BorderRadius.circular(10),
                ),
                padding: EdgeInsets.symmetric(vertical: 10),
                child: Text(
                  DemoLocalizations.of(context).SubmitSettings,
                  style: TextStyle(
                      color: Colors.white, fontWeight: FontWeight.bold),
                ),
              ),
            ),
          SizedBox(
            height: 24,
          ),
        ],
      ),
    );
  }

  void screeDate() {
    // 从此列表中删除满足的所有对象。
    currentDeviceAllDatas.singleArgs.removeWhere((item) {
      return item.itemTitle == singleTitle1;
    });
  }

  void registerLoginEvent() {
    Application.eventBus.on<getSetting_event>().listen((event) {
      // singleArgs
      currentDeviceAllDatas = demo.currentDeviceAllDatas;
      singleArgs = currentDeviceAllDatas.singleArgs;
      print(currentDeviceAllDatas.singleArgs);
      // setState(() {});
      if (!seesetting) {
        return;
      }
      seesetting = false;
      List<ShowSettingItem> itemValues = [];
      for (var value in currentDeviceAllDatas.singleArgs) {
        //筛选
        int index = currentDeviceAllDatas.singleArgs.indexOf(value);
        print(index);
        switch (deviceType) {
          case "11":
            //直流平移门
            if (index == 1 || index == 4 || index == 5) {
              continue;
            }
            break;
          case "22":
            //欢迎使用直流平开门
            break;
          case "33":
            //欢迎使用平开自动门
            break;
          case "55":
            // deviceName = "欢迎使用交流平移门";
            break;
          case "66":
            // deviceName = "欢迎使用交流平开门";
            break;
          case "77":
            // deviceName = "欢迎使用平移自动门";
            break;
          case "18":
            //模式二国内版
            break;
          case "28":
            // deviceName = "欢迎使用直流平开门";
            break;
          case "58":
            // deviceName = "欢迎使用交流平移门";
            break;
          case "68":
            // deviceName = "欢迎使用交流平开门";
            break;
          case "41":
            // deviceName = "欢迎使用1-4路点动开关";
            break;
          case "42":
            // deviceName = "欢迎使用1-4路自锁开关";
            break;
          case "43":
            // deviceName = "欢迎使用1-4路互锁开关";
            break;
          default:
            //case null:
            return;
        }

        for (int i = 0, length = value.itemTitle.length; i < length; i++) {
          ShowSettingItem settingItem = ShowSettingItem();
          settingItem.itemTitle = value.itemTitle.elementAt(i).toString();
          settingItem.itemValue = value.itemValue.elementAt(i).toString();
          //此处写细分逻辑
          // if (deviceType.toString() == "11") {
          //   if (i == 4 || i == 5 || i == 6 || i == 7 || i == 1) {
          //     continue;
          //   }
          // }
          itemValues.add(settingItem);
        }
      }

      MaterialPageRoute(builder: (context) => new RandomWords());
      Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
        return new RandomWords(models: itemValues);
      }));

      //        value.itemValue.forEach((f){
      //          //print(f);      //循环打印
      //          ShowSettingItem settingItem = ShowSettingItem();
      //          settingItem.itemTitle = f.toString();
      //          settingItem.itemValue = f.toString();
      //          itemValues.add(settingItem);
      //        });

      //       showDialog(
      //           context: context,
      //           barrierDismissible: false,
      //           builder: (BuildContext context) {
      //             return Scaffold(
      //               appBar: AppBar(
      //                 title: Text("Cites around world"),
      //               ),
      //               body: Padding(
      //                 padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 0.0),
      //                 child: ListView.builder(
      //                   itemBuilder: (BuildContext context, int index) {
      //                     return _getItemUI(context, index , itemValues);
      //                   },
      //                   itemCount: 24,
      //                 ),
      //               ),
      //             );
      //           });
      // changeUI();
    });
  }

  changeUI() async {
    setState(() {
      currentDeviceAllDatas = demo.currentDeviceAllDatas;
      print(currentDeviceAllDatas);
    });
  }

  //弹窗
  Future<void> getPopWindow(context, isSingleArg, itemIndex) {
    this.showItemWithType();

    singleArgs = [
      Item(_sliderValues1, singleTitle1),
      Item(_sliderValues2, singleTitle2),
      Item(_sliderValues3, singleTitle3),
      Item(_sliderValues4, singleTitle4),
      Item(_sliderValues5, doubleTitle1),
      Item(_sliderValues6, doubleTitle2)
    ];

    // var tempArr = isSingleArg ? singleArgs: doubleArgs;//临时数组 List<Item>
//    print("点击了" + itemIndex.toString());
    var tempArr = singleArgs;
    print(tempArr);
    var tempItem = tempArr[itemIndex]; //点击的哪一个item
    var tempTitleArr = tempItem.itemTitle; //当前点击的item 所对应的标题
    var title = DemoLocalizations.of(context).setdoor1;
    switch (itemIndex) {
      case 0:
        title = '设置门1开关门时间';
        title = DemoLocalizations.of(context).setdoor1;
        break;
      case 1:
        title = '设置门2开关门时间';
        title = DemoLocalizations.of(context).setforcedelay;
        break;
      case 2:
        title = '设置力度和关门延时时间';
        title = title_two;
        break;
      case 3:
        title = '设置开关门速度';
        title = DemoLocalizations.of(context).setdoorspeed;
        break;
    }
    // List<double> slideTempValues = [0,0,0,0];//当前点击的item 所对应的值
    List<double> slideTempValues = tempItem.itemValue;
    List<double> minList = [0, 0, 0, 0];
    List<double> maxList = [120, 120, 120, 120];
    switch (itemIndex) {
      case 0:
        break;
      case 1:
        break;
      case 2:
        minList = [0, 0, 0, 0, 0];
        maxList = [255, 5, 5, 3.3, 3.3];
        print("deviceType:" + deviceType);
        if (deviceType == '66') {
          // minList = [0, 0, 0, 1, 1];

          // maxList = [255, 5, 5, 33, 33];
        }
        break;
      case 3:
        minList = [85.0, 50, 85, 50];
        maxList = [100, 65, 100, 65];
        break;
    }

    return showDialog<void>(
      context: context,
      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return StatefulBuilder(
          builder: (context, state) {
            return AlertDialog(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(15))),
              title: Container(
                child: Text(
                  title,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                      color: Colors.blueAccent, fontWeight: FontWeight.bold),
                ),
              ),

              // Wrap(
              //   children: <Widget>[
              //     //                  Icon(Icons.security,color: Colors.blueAccent,),

              //   ],
              // ),
              content: Container(
                padding: EdgeInsets.symmetric(horizontal: 2),
                constraints: BoxConstraints(
                  maxHeight: MediaQuery.of(context).size.height / 1.8,
                ),
                // height: MediaQuery.of(context).size.height / 1.9,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(5),
                ),
                child: SingleChildScrollView(
                  child: Column(
                    children: List.generate(tempTitleArr.length, (index) {
                      return Visibility(
                        maintainSize: false,
                        visible: tempTitleArr[index] != "null",
                        maintainState: false,
                        maintainAnimation: false,
                        child: Column(
                          children: <Widget>[
                            Container(
                              height: 0.5,
                              color: Colors.grey[200],
                            ),
                            ListTile(
                              //                              title: Text('${tempTitleArr[index]}'),
                              //                              subtitle: Text('${slideTempValues[index]}'),
                              title: new Row(
                                children: <Widget>[
                                  Expanded(
                                    child: new Text(
                                      '${tempTitleArr[index]}' +
                                          ":" +
                                          (itemIndex == 2
                                              ? index == 3 || index == 4
                                                  ? '${slideTempValues[index].toString().substring(0, 3)}'
                                                  : '${slideTempValues[index].toString().substring(0, slideTempValues[index].toString().indexOf("."))}'
                                              : '${slideTempValues[index].toString().substring(0, slideTempValues[index].toString().indexOf("."))}'),
                                      // maxLines: 2,
                                      textAlign: TextAlign.center,
                                    ),
                                  ),
                                  // new Text(
                                  //   itemIndex == 2
                                  //       ? index == 3 || index == 4
                                  //           ? '${slideTempValues[index].toString().substring(0, 3)}'
                                  //           : '${slideTempValues[index].toString().substring(0, slideTempValues[index].toString().indexOf("."))}'
                                  //       : '${slideTempValues[index].toString().substring(0, slideTempValues[index].toString().indexOf("."))}',
                                  //   textAlign: TextAlign.center,
                                  //   style: TextStyle(
                                  //       color: Colors.orangeAccent,
                                  //       fontWeight: FontWeight.bold),
                                  // ),
                                ],
                              ),
                              //                                  leading: Icon(Icons.timer),
                            ),
                            Slider(
                              value: slideTempValues[index],
                              onChanged: (data) {
                                state(() {
                                  print("data:" + data.toString());
                                  slideTempValues[index] = data;
                                });
                              },
                              onChangeStart: (data) {},
                              onChangeEnd: (data) {
                                print('endff:$data');
                                if (itemIndex == 2) {
                                  if (index == 3 || index == 4) {
                                    if (deviceType == '66') {
                                      // data = double.parse(
                                      //     (data * 10).toStringAsFixed(4));
                                      // data = double.parse((data * 1 - 0)
                                      //     .toString()
                                      //     .substring(0, 3));
                                      data = double.parse(
                                          data.toString().substring(0, 3));
                                      print('endhhh:$data');
                                    } else {
                                      data = double.parse(
                                          data.toString().substring(0, 3));
                                    }
                                  }
                                }
                                //                                _sliderValue = data;
                                slideTempValues[index] = data; //拿到最后值了。
                                //if(isSingleArg){
                                singleArgs[itemIndex] =
                                    Item(slideTempValues, tempTitleArr);
                                //                                  print(slideTempValues);
                                //                                  print(singleArgs);
                                currentDeviceAllDatas = Data(singleArgs, []);
                                //                                }else{
                                //                                  doubleArgs[itemIndex]=Item(slideTempValues,tempTitleArr);
                                //                                  currentDeviceAllDatas= Data([],doubleArgs);
                                //                                }
                                // 此处将页面刷新 刷新后根据数据界面重新绘制
                                print('endgg:$data');

                                setState() {
                                  // _receiveNotification = now;
                                }
                                state(() {});
                              },
                              min: minList[index],
                              max: maxList[index],
                              divisions: itemIndex == 2
                                  ? index == 3 || index == 4
                                      ? deviceType == '66'
                                          ? (maxList[index] - minList[index])
                                                  .toInt() *
                                              10
                                          : (maxList[index] - minList[index])
                                                  .toInt() *
                                              10
                                      : (maxList[index] - minList[index])
                                          .toInt()
                                  : (maxList[index] - minList[index]).toInt(),
                              label: itemIndex == 2
                                  ? index == 3 || index == 4
                                      ?
                                      // deviceType == '66'
                                      //     ? slideTempValues[index]
                                      //         .toStringAsFixed(2)
                                      //     :
                                      double.parse(slideTempValues[index]
                                              .toString()
                                              .substring(0, 3))
                                          .toString()
                                      : slideTempValues[index]
                                          .toInt()
                                          .toString()
                                  : slideTempValues[index].toInt().toString(),
                              activeColor: Colors.blueAccent,
                              inactiveColor: Colors.grey,
                              semanticFormatterCallback: (double newValue) {
                                return '${newValue.round()} dollars}';
                              },
                            ),
                            Container(
                              height: 1.0,
                              color: Colors.grey[300],
                            ),
                          ],
                        ),
                      );
                    }),
                  ),
                ),
              ),
              actions: <Widget>[
                FlatButton(
                  child: Text(DemoLocalizations.of(context).cancel),
                  textColor: Colors.grey[600],
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
                FlatButton(
                  child: Text(DemoLocalizations.of(context).confirm),
                  onPressed: () {
                    //TODO 全部拿到的数据,点击确定按钮发送网络请求
                    print(currentDeviceAllDatas);
                    _showsliderValue = [0.0, 0.0, 0.0, 0.0]; //重置临时数组
                    Navigator.of(context).pop();
                  },
                ),
              ],
            );
          },
        );
      },
    );
  }

  void showItemWithType() {
    if (deviceType == "22") {
      mener = true;
    }
    mener = true;
    liduheyanshi = true;
    guanmensudu = true;

    singleTitle1 = [
      DemoLocalizations.of(context).singleTitle1,
      DemoLocalizations.of(context).singleTitle2,
      DemoLocalizations.of(context).singleTitle3,
      DemoLocalizations.of(context).singleTitle4
    ];

    singleTitle2 = [
      DemoLocalizations.of(context).doubleTitle1,
      DemoLocalizations.of(context).doubleTitle2,
      DemoLocalizations.of(context).doubleTitle3,
      DemoLocalizations.of(context).doubleTitle4
    ];

    singleTitle3 = [
      DemoLocalizations.of(context).Title1,
      DemoLocalizations.of(context).Title2,
      DemoLocalizations.of(context).Title3,
      DemoLocalizations.of(context).Title4,
      DemoLocalizations.of(context).Title5,
    ];

    singleTitle4 = [
      DemoLocalizations.of(context).Title6,
      DemoLocalizations.of(context).Title7,
      DemoLocalizations.of(context).Title8,
      DemoLocalizations.of(context).Title9,
    ];

    print(deviceType);
    // deviceType = '22';
    title_two = DemoLocalizations.of(context).setforcedelay;

    switch (deviceType) {
      case "11":
      case "18":
        //直流平移门
        singleTitle3 = ['关门延时时间', 'null', 'null', '遇阻回弹力', '停门检测力'];
        singleTitle3 = [
          DemoLocalizations.of(context).Title1,
          'null',
          'null',
          DemoLocalizations.of(context).Title4,
          DemoLocalizations.of(context).Title5,
        ];
        break;
      case "22":
      case "28":
        //直流平开门
        men2 = true;
        men2time = true;
        title_two = DemoLocalizations.of(context).setforcedelayAndErrorTile;
        break;
      case "33":
        singleTitle3 = ['关门延时时间', 'null', 'null', '遇阻回弹力', '停门检测力'];
        singleTitle3 = [
          DemoLocalizations.of(context).Title1,
          'null',
          'null',
          DemoLocalizations.of(context).Title4,
          DemoLocalizations.of(context).Title5,
        ];
        break;
      case "55":
      case "58":
        singleTitle3 = ['关门延时时间', 'null', 'null', '遇阻回弹力', '停门检测力'];
        singleTitle3 = [
          DemoLocalizations.of(context).Title1,
          'null',
          'null',
          DemoLocalizations.of(context).Title4,
          DemoLocalizations.of(context).Title5,
        ];
        break;
      case "66":
      case "68":
        men2 = true;
        men2time = true;
        title_two = DemoLocalizations.of(context).setforcedelayAndErrorTile;

        break;
      case "77":
        mener = false;
        men_senven = true;
        singleTitle3 = ['关门延时时间', 'null', 'null', '遇阻回弹力', '停门检测力'];

        singleTitle3 = [
          DemoLocalizations.of(context).Title1,
          'null',
          'null',
          'null',
          'null',
        ];
        break;
    }
  }

  void showInputNewAP(context) {
    showCupertinoDialog(
        context: context,
        builder: (context) {
          return CupertinoAlertDialog(
            title: Text(DemoLocalizations.of(context).ModifyAP),
            content: Card(
              elevation: 0.0,
              child: Column(
                children: <Widget>[
                  TextField(
                    controller: _ApNamecontroller,
                    inputFormatters: [
                      WhitelistingTextInputFormatter(RegExp("[a-z]")), //只允许输入字母
                    ],
                    decoration: InputDecoration(
                        hintText: 'input new ApName',
                        filled: true,
                        fillColor: Colors.grey.shade50),
                  ),
                  TextField(
                    controller: _ApPasswcontroller,
                    keyboardType: TextInputType.text,
                    decoration: InputDecoration(
                        hintText: 'input new ApPassw',
                        filled: true,
                        fillColor: Colors.grey.shade50),
                  ),
                ],
              ),
            ),
            actions: <Widget>[
              CupertinoDialogAction(
                onPressed: () {
                  Navigator.pop(context);
                  _ApNamecontroller.text = null;
                  _ApPasswcontroller.text = null;
                },
                child: Text(DemoLocalizations.of(context).cancel),
              ),
              CupertinoDialogAction(
                onPressed: () {
                  String name = _ApNamecontroller.text.toString();
                  String password = _ApPasswcontroller.text.toString();

                  if (name == null ||
                      password == null ||
                      name.length < 1 ||
                      password.length < 8) {
                    showToast(DemoLocalizations.of(context).enterpassword);
                    return;
                  }
                  final MqttClientPayloadBuilder builder =
                      MqttClientPayloadBuilder();
                  print(name);
                  print(password);
                  builder.addByte(0x41);
                  builder.addByte(0x50);
                  builder.addByte(name.length);
                  builder.addByte(password.length);
                  builder.addString(name);
                  builder.addString(password);
                  //                  var namebytr = Utf8Encoder().convert(name);
                  //                  var passwordbyte = Utf8Encoder().convert(password);
                  widget.model.wifiname = name;
                  widget.model.wifipassword = password;
                  ApiService().ESPEDIT((Response response) {
                    if (response != null) {
                      ConmmonResult conmmonResult =
                          ConmmonResult.fromJson(response.data);
                      if (conmmonResult.success) {
                        showToast(conmmonResult.message);
                      } else {
                        showToast(conmmonResult.message);
                      }
                    }
                  }, widget.model);
                  demo.publishMessage(builder);
                  Navigator.pop(context);
                },
                child: Text(DemoLocalizations.of(context).confirm),
              ),
            ],
          );
        });
  }

  @override
  bool get wantKeepAlive => true;

  void getSettingStatus() {
    final MqttClientPayloadBuilder builder = MqttClientPayloadBuilder();
    builder.addByte(72);
    builder.addByte(1);
    builder.addByte(10);
    builder.addByte(11);
    demo.publishMessage(builder);
  }

  void showLoding() {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          Future.delayed(Duration(milliseconds: 300)).then((e) {
            Navigator.pop(context);
          });
          return new LoadingDialog(
            text: DemoLocalizations.of(context).GettingSettings,
          );
        });
  }
}

Widget _getItemUI(
    BuildContext context, int index, List<ShowSettingItem> itemValues) {
  return Card(
    child: Column(
      children: <Widget>[
        ListTile(
//          leading: Image.asset(
//            currentDeviceAllDatas.,
//            fit: BoxFit.cover,
//            width: 100.0,
//          ),
            title: Text(
              itemValues[index].itemTitle,
              style: TextStyle(fontSize: 14.0, fontWeight: FontWeight.bold),
            ),
            subtitle: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  itemValues[index].itemTitle,
                  style: TextStyle(
                    fontSize: 13.0,
                  ),
                ),
                Text(
                  "Population: ${itemValues[index].itemTitle}",
                  style: TextStyle(
                    fontSize: 11.0,
                  ),
                ),
              ],
            ))
      ],
    ),
  );
}
